//
//  SwiftUIView1.swift
//  DemoUI
//
//  Created by 白瑾浩 on 2022/12/1.
//

import SwiftUI

struct SwiftUIView2: View {
    private let title1:String
    private let subtitle:String
    init(title:String, subtitle:String){
        self.title1 = title
        self.subtitle = subtitle
    }
    
    var body: some View {
        VStack {
            Text(self.subtitle)
                .padding( 10 )
                .foregroundColor( Color.red )
                .font( Font.system(size:  20 ) )
            
            NavigationLink(destination: ViewControllerRepresentation().navigationTitle("手写界面")
                           
            ) {
                Text("跳转到UIViewController").frame(width: nil, height: 31, alignment: .center)
            }
            
            NavigationLink(destination:ContentView()) {
                Text("页面的跳转和environment使用").frame(width: nil, height: 31, alignment: .center)
            }
            
            NavigationLink(destination:DefaultNavigation()) {
                Text("Navigation标题设置").frame(width: nil, height: 31, alignment: .center)
            }
            
            
            
        }.navigationTitle( self.title1 )
            .onAppear(){
                print( "页面展示" )
            }.onDisappear(){
                print( "页面销毁" )
            }
        
        
    }
    
    
}

struct SwiftUIView2_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView1()
    }
    
}

struct ViewControllerRepresentation: UIViewControllerRepresentable {
    typealias UIViewControllerType = UIViewController
    
    func makeUIViewController(context: UIViewControllerRepresentableContext<ViewControllerRepresentation>) -> ViewControllerRepresentation.UIViewControllerType {
        //跳转到手写界面
        return ThreeViewController()
        //故事板
        //UIStoryboard(name: "Storyboard", bundle: nil).instantiateViewController(identifier: "ViewController") as! ViewController
    }
    
    func updateUIViewController(_ uiViewController: ViewControllerRepresentation.UIViewControllerType, context: UIViewControllerRepresentableContext<ViewControllerRepresentation>) {
        print( "我返回了" )
    }
}


//结果视图
//environment传值
//第一种
class User: ObservableObject {
    @Published var score = 0
}
//第二种
private struct MyEnvironmentKey:EnvironmentKey{
    static let defaultValue :String = "baijinhao"
}
//自定义多个environmentkey
struct Setting {
    var username: String = ""
    var isSoundOn: Bool = false
    init(name: String) {
        self.username = name
    }
}
struct DefineMyEnvironmentKey:EnvironmentKey{
    static var defaultValue :Setting {
        return Setting(name: "xiaobai")
    }
}
extension EnvironmentValues {
    var customSetting: Setting {
        get { return self[DefineMyEnvironmentKey.self] }
        set { self[DefineMyEnvironmentKey] = newValue }
    }
}


struct ContentView: View {
    @State var  count:Int = 0
    //environment传值
    @StateObject var user = User()
    var body: some View {
        //        NavigationView {
        VStack(spacing: 30) {
            Button { [self]
                self.count = self.count + 1
            } label: {
                Text("点击")
            }
            
            Text("You're going to flip a coin – do you want to choose heads or tails?\(count)")
            
            Text("You're going to flip a coin – do you want to choose heads or tails?\(count)")
            
            Text("environment传值的变化\(user.score)")
            
            NavigationLink(destination: ResultView(choice: "Heads")
                .environmentObject( user )
                .environment(\.customSetting, Setting(name: "我是environment传值了1"))
                           
            ) {
                Text("environment传值的变化")
                
            }
            
            NavigationLink(destination: ResultView(choice: "Tails")
                .environmentObject( user )
                .environment(\.customSetting, Setting(name: "我是environment传值了2"))
            ) {
                Text("Choose Tails")
            }
        }
        
        //     }
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
    
}

struct ResultView: View {
    var choice: String
    //environment 传值
    @EnvironmentObject var user: User
    @Environment(\.colorScheme) var colorScheme
    @Environment(\.customSetting) var customSetting
    var body: some View {
        VStack{
            Text("You chose \(choice)")
            Button {
                self.user.score = self.user.score +  1
                
            } label: {
                Text("点击改变environment的值")
                    .foregroundColor( colorScheme == .light ? .yellow : .blue )
            }
            Text("customSetting\(customSetting.username)")
            
        }
    }
}

struct ResultView_Previews: PreviewProvider {
    static var previews: some View {
        ResultView(choice: "s")
    }
    
}

//自定义导航
struct DefaultNavigation:View{
    @State private var score = 0
    @State private var isFullScrenn = false
    @State var show = true
    var body: some View {
        VStack{
            Text("自定义导航\(self.score)")
            Button {
                isFullScrenn = !isFullScrenn
            } label: {
                Text("是否全屏，隐藏导航")
            }
            //显示和隐藏
            if show {
                Text("Hello, world!")
                    .padding()
                    .task{
                        var i = 0
                        while !Task.isCancelled {  // 尝试将本行代码改成 while true {
                            try? await Task.sleep(nanoseconds: 1_000_000_000)
                            print("task:",i)
                            i += 1
                        }
                    }
                    .onAppear{
                        Task{
                            var i = 0
                            while !Task.isCancelled {
                                try? await Task.sleep(nanoseconds: 1_000_000_000)
                                print("appear:",i)
                                i += 1
                            }
                        }
                    }
                
            }
            Button("show"){
                show.toggle()
            }
            
        }
        .navigationBarTitle("我是导航")
        .navigationBarHidden( isFullScrenn )
        .navigationBarItems(
            //左边
            leading: HStack{
                Button("minus 1") {
                    self.score -= 1
                }
                Button("minus 2") {
                    self.score -= 1
                }
            },
            //右边
            trailing:
                HStack{
                    Button("Add 1") {
                        self.score += 1
                    }
                    Button("Add 2") {
                        self.score += 1
                    }
                }
        )
        .statusBarHidden( isFullScrenn )
    }
    
}
struct DefaultNavigation_Previews: PreviewProvider {
    static var previews: some View {
        DefaultNavigation()
    }
    
}
